package com.itheima.service.impl;

import com.aliyun.oss.ServiceException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.HttpRequestMethodNotSupportedException;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description: ClazzServiceImpl
 * @Author: HH
 * @Date: 2025/4/19 10:32
 **/

/**
 * 学员管理ServiceImpl
 */
@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 条件分页查询
     *
     * @param param
     * @return
     */
    @Override
    public PageBean page(ClazzQueryParam param) {
        // 1. 设置分页参数
        PageHelper.startPage(param.getPage(), param.getPageSize());

        // 2. 执行查询
        // 2.1 根据所给内容查询符合条件的班级列表
        List<Clazz> clazzList = clazzMapper.list(param);
        // 2.2 根据查询到的班级列表去获取对应开班状态
        for (Clazz clazz : clazzList) {
            // 根据当前时间判断开班状态
            LocalDate time = LocalDate.now();
            if (time.isAfter(clazz.getBeginDate()) && time.isBefore(clazz.getEndDate())) {
                clazz.setStatus("在读");
            } else if (time.isAfter(clazz.getEndDate())) {
                clazz.setStatus("已结课");
            } else {
                clazz.setStatus("未开班");
            }
        }
        // 3. 解析封装分页结果
        Page<Clazz> p = (Page<Clazz>) clazzList;
        return new PageBean(p.getTotal(), p.getResult());
    }

    /**
     * 新增班级
     *
     * @param clazz
     */
    @Override
    public void insert(Clazz clazz) {
        // 设置创建时间与更新时间
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        System.out.println(clazz);
        clazzMapper.insert(clazz);
    }

    /**
     * 根据id查询班级
     *
     * @param id
     * @return
     */
    @Override
    public Clazz getInfo(Integer id) {
        return clazzMapper.getById(id);
    }

    /**
     * 修改班级
     *
     * @param clazz
     */
    @Override
    public void update(Clazz clazz) {
        // 设置基础信息
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }

    /**
     * 查询班级列表
     *
     * @return
     */
    @Override
    public List<Clazz> list() {
        return clazzMapper.listAll();
    }

    /**
     * 删除班级
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        // 1. 根据班级id查询是否分配有学生
        int count = studentMapper.countByClazzId(id);
        if (count > 0) {
            throw new ServiceException("对不起, 该班级下有学生, 不能直接删除");
        } else {
            clazzMapper.deleteById(id);
        }
    }
}
